package com.jeffry.redial;

import com.jeffry.redial.Utils.Constants;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ReDialActivity extends ListActivity {

    private static final int MENU_ADD_SELECT_NUMBERS = 0;

    private List<String> mNumbersList;
    private ArrayAdapter<String> mAdapter;

    private Button mDialButton;
    private ListView mListView;

    private OnClickListener mDialClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            String phoneNumber = mListView.getItemAtPosition(mListView
                    .getCheckedItemPosition()).toString();
            Uri uri = Uri.parse("tel:" + phoneNumber);
            Intent in = new Intent(Intent.ACTION_CALL, uri);
            startActivity(in);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mDialButton = (Button) findViewById(R.id.dial_btn);
        mDialButton.setOnClickListener(mDialClickListener);

        mNumbersList = Utils.getNumbersList(this);
        mAdapter = new ArrayAdapter<String>(this,
                R.layout.simple_list_item_single_choice, R.id.text1, mNumbersList);
        mListView = getListView();
        mListView.setAdapter(mAdapter);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case Constants.REQUEST_NUMBERS_LIST:
                if (resultCode == Activity.RESULT_OK) {
                    ArrayList<String> list = (ArrayList<String>) data
                            .getSerializableExtra(Constants.EXTRA_NUMBERS_LIST);
                    if (list != null && list.size() > 0) {
                        mAdapter.clear();
                        for (String s : list) {
                            mAdapter.add(s);
                        }
                    }
                }
                break;
            default:
                break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_ADD_SELECT_NUMBERS, 0, "Add/Select");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case MENU_ADD_SELECT_NUMBERS:
                Intent in = new Intent(this, DialNumbersListActivity.class);
                startActivityForResult(in, Constants.REQUEST_NUMBERS_LIST);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}